package com.epam.iunna.bookshop.model;

/**
 * @author Iunna
 */
import java.sql.*;

public class MySQLDatabase {
	public static void main(String[] args) {
		Connection conn = null;

		try {
			// SETTINGS: userName, passWord and dataBase.
			String userName = "root";
			String passWord = "1w1llfollowu";
			String dataBase = "BookStore";

			// Creating class.
			Class.forName("com.mysql.jdbc.Driver").newInstance();

			// Let's create a connection.
			conn = DriverManager.getConnection("jdbc:mysql:///" + dataBase,
					userName, passWord);

			// Do we have a connection?
			if (!conn.isClosed()) {
				// We have a MySQL Connection.
				Statement s = conn.createStatement();
				s.executeQuery("SELECT * FROM user");
				ResultSet rs = s.getResultSet();
				while (rs.next()) // loop through rows of result set
				{
					Long id = rs.getLong(1);
					String login = rs.getString(2);
					String pass = rs.getString(3);
					String name = rs.getString(4);
					String surname = rs.getString(5);
					String address = rs.getString(6);
					Long tel = rs.getLong(7);
					String email = rs.getString(8);
					Boolean banned = rs.getBoolean(9);
					System.out.println("uID: " + id + ", Login: " + login
							+ ", Password: " + pass + ", Name: " + name
							+ ", Surname: " + surname + ", Address: " + address
							+ ", tel: " + tel + ", email: " + email
							+ ", isBanned: " + banned);
				}
				rs.close(); // close result set
				s.close(); // close statement
			}
		} catch (Exception e) {
			System.err.println("Exception: " + e.getMessage());
		} finally {
			// If we don't have a error, close the connection!
			try {
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {

			}
		}
	}

}